<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<!DOCTYPE HTML>
<html>
<head>
    <base href="<%=basePath%>">
    <title></title>
    <meta http-equiv="cache-control" content="no-cache">

    <link rel="stylesheet" type="text/css" href="/css/edittable.css"  ></link>
    <link rel="stylesheet" type="text/css" href="/css/validate.css"  ></link>
    <script type="text/javascript"  src="/js/jquery-1.8.0.js"></script>

    <script>
        $(function(){
            $("input[type=text],textarea").focus(function(){
                $(this).addClass("input_focus");
            }).blur(function(){
                $(this).removeClass("input_focus");
            });

            $(".form_btn").hover(function(){
                    $(this).css("color","red").css("background","#6FB2DB");
                },

                function(){
                    $(this).css("color","#295568").css("background","#BAD9E3");
                });
        });


        function validate(){
            var result = true;

            if(accountFormat()==false){
                result = false;
            }else if(checkAdminName()==false){
                result = false;
            }
            else{
                result=confirm("确认要提交吗");
            }
            return result;
        }

        function checkAdminName(){
            var result=true;
            var adminName = $("#adminName").val();
            $.ajax({
                url:"AdminServlet",
                type:"post",
                async:false,
                data:{flag:"checkAdminName",adminName:adminName},
                success:function (data){
                    if(data=="has"){
                        result=false;
                        alert("账号已被注册")
                    }
                }
            });
            return result;
        }

        function accountFormat(){
            var result=true;
            var adminName = $("#adminName").val();
            var password = $("#password").val();
            if(adminName.length>15||adminName.length<4){
                result=false;
                alert("用户名不符合要求");
            }else {
                for (var i = 0; i < adminName.length; i++) {
                    var charCode = adminName.charCodeAt(i);
                    if (!(charCode >= 97 && charCode <= 122) && !(charCode >= 48 && charCode <= 57)) {
                        result = false;
                        alert("用户名不符合要求");
                        break;
                    }
                }
            }
            if(result==true) {
                if (password.length > 20 || password.length < 6) {
                    result = false;
                    alert("密码不符合要求");
                } else {
                    for (var i = 0; i < password.length; i++) {
                        var charCode = password.charCodeAt(i);
                        if (!(charCode >= 97 && charCode <= 122) && // a-z
                            !(charCode >= 65 && charCode <= 90) &&   // A-Z
                            !(charCode >= 48 && charCode <= 57)) {   // 0-9
                            result = false;
                            alert("密码不符合要求");
                            break;
                        }
                    }
                }
            }
            if(result==true){
                var repeatPassword=$("#pwdconfirm").val();
                if (repeatPassword!=password){
                    result = false;
                    alert("两次密码不一致");
                }
            }
            return result;
        }
    </script>

</head>

<body>
    <div class ="div_title">
        <div class="div_titlename"> <img src="/images/san_jiao.gif" ><span>管理员添加</span></div>
    </div>

    <form action="AdminServlet" method="post" onsubmit="return validate()" >
        <input type="hidden" name="flag" value="add" >

        <table class="edit_table" >
            <tr>
                <td class="td_info">用户账号:</td>
                <td class="td_input_short">
                    <input type="text" class="txtbox" id="adminName" name="adminName" value="${param.adminName }"/>
                </td>
                <td>
                    <label class="validate_info" id="adminName_msg">4-15位；只限数字(0-9)和英文(a-z),不区分大小写</label>
                </td>
            </tr>
            <tr>
                <td class="td_info">用户密码:</td>

                <td>
                    <input type="text"  class="txtbox"  name="password"  id="password" value="${param.password }"/>
                </td>
                <td><label  class="validate_info" id="password_msg" >数字或英文,6-20位</label></td>
            </tr>
            <tr>
                <td class="td_info">重复密码:</td>
                <td><input type="text" class="txtbox" id="pwdconfirm" value="${param.password }" />
                </td>
                <td><label  class="validate_info"  id="pwdconfirm_msg">请重复输入密码</label></td>
            </tr>
            <tr>
                <td class="td_info">备注信息:</td>
                <td><textarea rows="4" cols="27" name="note" class="txtarea" >${param.note }</textarea> </td>
                <td><label></label></td>
            </tr>
            <tr>
                <td class="td_info"> </td>
                <td>
                    <input class="form_btn" type="submit" value="提交" />
                    <input type="reset"  class="form_btn" value="重置" /> </td>
                <td>
                    <label id="result_msg" class="result_msg">${msg}</label>
                </td>
            </tr>
        </table>
    </form>

</body>
</html>